package com.fzy.core;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.webwork.dispatcher.DispatcherUtils;
import com.opensymphony.webwork.dispatcher.ServletDispatcher;
import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory;
import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
import com.opensymphony.xwork.ActionProxyFactory;

public class MyServletDispatcher extends ServletDispatcher {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7871738781153262216L;
	
	

	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		
        // add by zhx
        ActionProxyFactory.setFactory(new MyActionProxyFactory());
	}



	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
        // prepare the request no matter what - this ensures that the proper character encoding
        // is used before invoking the mapper (see WW-9127)
		
        DispatcherUtils du = DispatcherUtils.getInstance();
        du.prepare(request, response);

        try {
            request = du.wrapRequest(request, getServletContext());
        } catch (IOException e) {
            String message = "Could not wrap servlet request with MultipartRequestWrapper!";
            LOG.error(message, e);
            throw new ServletException(message, e);
        }
        
        ActionMapping mapping = ActionMapperFactory.getMapper().getMapping(request);
        if (mapping == null) {
            try {
                response.sendError(404);
            } catch (IOException e) {
                LOG.error("Could not send 404 after not finding any ActionMapping", e);
            }
            return;
        }

        du.serviceAction(request, response, getServletContext(), mapping);
	}

}
